package calificaciones;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.net.ParseException;
import android.os.AsyncTask;
import android.util.Log;

public class ValidacionPasedeLista {

	
	link l =new link();
	String []calificacion;
	 JSONArray jArray=null;
		String result = null;
		InputStream is = null;
		StringBuilder sb = null;
		public ConectarSam mAuthTask =new ConectarSam();
		String mtID,mt,unID;
		public static String CalificacionA;
		
		
		public static List<String> Lcalificacion =new ArrayList<String>(); 
	
	
	
	
	
	public void conectar(String m,String u){
		mtID=m;
		unID=u;
		mAuthTask.execute((Void)null);
		
	}
	
	
	
	

	public class ConectarSam extends AsyncTask<Void, Void, Boolean> {
		@Override
		public Boolean doInBackground(Void... params) {
			ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
			nameValuePairs.add(new BasicNameValuePair("id",String.valueOf(mtID)));
					nameValuePairs.add(new BasicNameValuePair("idUnidad",String.valueOf(unID)));
					
		
			try {
				HttpClient httpclient = new DefaultHttpClient();
				Log.v("log_tag ",l.linkvalidacionAsistencia);
				HttpPost httppost = new HttpPost(l.linkvalidacionAsistencia);
				
				httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
				HttpResponse response = httpclient.execute(httppost);
				HttpEntity entity = response.getEntity();
				is = entity.getContent();
			} catch (Exception e) {
				Log.e("log_tag", " "
						+ e.toString());
			}
			try {
				BufferedReader reader = new BufferedReader(
						new InputStreamReader(is, "iso-8859-1"), 8);
				sb = new StringBuilder();
				sb.append(reader.readLine() + "\n");

				String line = "0";
				while ((line = reader.readLine()) != null) {
					sb.append(line + "\n");
				}
				is.close();
				result = sb.toString();
				Log.v(" obtenido",result);
			} catch (Exception e) {
				Log.e("log_tag",
						"Error convirtiendo datos recibidos Calificacion Consulta " + e.toString());
			}
			
		
			try {
				
				JSONObject json = new JSONObject(result);
				
				int success = json.getInt("success");	
				
				if(success==1){
					jArray = json.getJSONArray("tipo");
					JSONObject json_data = null;	
					for(int i=0;i<jArray.length();i++){
						json_data = jArray.getJSONObject(i);
						CalificacionA=json_data.getString("estado_id");
						Lcalificacion.add(""+CalificacionA);
						Log.e("Result ", ""+Lcalificacion.get(i));
					}
					llenar();	
					
				}else{
					
				}
				Log.e("Result ", ""+success);
					
					
	
	//
	
			} catch (JSONException e1) {
				Log.e("JSON ERR", e1.getMessage());
			} catch (ParseException e1) {
				e1.printStackTrace();
			}
			return true;
		}
		
	}
	
	
	
	public String llenar(){
		
		String a=CalificacionA;
		return a;
	}
	
	
	public void limpiar(){
		Lcalificacion.clear();
	}
}
